<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends Controller {
	var $data;
	
	function Admin()
	{
		parent::Controller();

		$this-> db-> query('SET NAMES utf8');
		header("Content-Type: text/html; charset=UTF-8"); 

		$this->form_validation->set_message('signup', 'Введены неверные данные');
		//Lang
		$lang = $this->uri->segment(1); 
		if($lang == 'en') {
			$this->config->set_item('language', 'english');
		} else {
			$this->config->set_item('language', 'russian');
		}
		$this->lang->load('main');
		$this->data['lang'] = $this->lang;
		//
		
		//Conf
		$q = $this->db->get('conf');
		if ($q -> num_rows() > 0) {
			foreach ($q -> result() as $row) {
				$conf[$row->var] = $row->val;
			}
		}
		$this->data['conf'] = &$conf;
		$this->data['userPerm'] = &$this->userperm;
		$this->data['status'] = &$this->session->userdata('status');
		$this->session->unset_userdata('status');
		//
	}
	
	function logout()
	{
		$this->userperm->logout();
		redirect();
	}
	
	function profile()
	{
	if( $this->input->post('nick') ){
		if( $this->form_validation->run('userProfileUp') ){
			if($this->userperm->edit($this->session->userdata('user_id'), $this->input->post('nick') )) {
				$this->session->set_flashdata('status', 'Профайл обновлён');
				redirect('user/profile');
			}
		}else{
			$this->session->set_flashdata('status', $this->form_validation->_error_messages['signup']);
		}
	}
	if( $this->input->post('password') ){
		if( $this->form_validation->run('userProfilePass') ){
			if($this->userperm->edit($this->session->userdata('user_id'),'', $this->input->post('password'), $this->input->post('rePassword') )) {
				$this->session->set_flashdata('status', 'Пароль изменён') ;
				redirect('user/profile');
			}
		}else{
			$this->session->set_flashdata('status', $this->form_validation->_error_messages['signup']);	
		}
	}
	
		$this->data['type'] = 'userProfile';
		//Menu
		$this->load->model('menu','',TRUE);
		$this->data['menu'] = $this->menu->get(); 
		//
		$this->load->view('main',$this->data);
	}
	
	function reg()
	{	
	if( $this->input->post('email') ){
		if( $this->form_validation->run('signup') ){
			if($this->userperm->create($this->input->post('email'), $this->input->post('password') )) {
				$this->session->set_userdata('status', 'Регистрация завершенна') ;
				redirect();
			}else{
				$this->session->set_flashdata('status','Такой email уже используется');
			}
		}else{
			$this->session->set_flashdata('status',$this->form_validation->_error_messages['signup']);
		}
	}
	
		//Menu
		$this->load->model('menu','',TRUE);
		$this->data['menu'] = $this->menu->get(); 
		//
		
		//View
		$this->data['type'] = 'userReg';
		$this->load->view('main',$this->data);
	}
	
	function matchingCaptcha($str)
	{
		$this->load->library('si_captcha');
		return $this->si_captcha->check($str);
	}
}

/* End of file main.php */
/* Location: ./system/application/controllers/main.php */
